import { getCartList, updateCartNum, delSelect } from '@/api/cart'
import { getAddressList, getDefaultAddrId, getRegions, setDefaultAddress } from '@/api/pay'
import { Toast } from 'vant'
export default {
  namespaced: true,
  state () {
    return {
      cartList: [],
      addressList: [],
      defaultAddressId: null,
      regions: []
    }
  },
  actions: {
    async getCartAction (context) {
      const { data } = await getCartList()
      data.list.forEach(item => {
        item.isChecked = true
      })
      context.commit('setCartList', data.list)
    },
    async updateNumAction (context, params) {
      const { goodsId, skuId, value } = params
      context.commit('updateCartNum', { goodsId, value })
      await updateCartNum(goodsId, value, skuId)
    },
    async cartDelAction (context) {
      const selCartList = context.getters.selCartList
      const cartIds = selCartList.map(item => item.id)
      await delSelect(cartIds)
      context.dispatch('getCartAction')
    },
    async getAddressList (context) {
      const { data: { list } } = await getAddressList()
      context.commit('setAddressList', list)
    },
    async getDefaultAddrId (context) {
      const { data: { defaultId } } = await getDefaultAddrId()
      context.commit('setDefaultAddrId', defaultId)
    },
    async getRegions (context) {
      const { data: { list } } = await getRegions()
      context.commit('setRegions', list)
    },
    async setDefaultAction (context, id) {
      await setDefaultAddress(id)
      Toast('默认配送地址设置成功')
      context.commit('setDefaultAddrId', id)
    }
  },
  mutations: {
    setCartList (state, data) {
      state.cartList = data
    },
    toggleAllCheck (state, flag) {
      state.cartList.forEach(item => {
        item.isChecked = flag
      })
    },
    toggleCheck (state, goodsId) {
      const goods = state.cartList.find(item => item.goods_id === goodsId)
      goods.isChecked = !goods.isChecked
    },
    updateCartNum (state, { goodsId, value }) {
      const goods = state.cartList.find(item => item.goods_id === goodsId)
      goods.goods_num = value
    },
    setAddressList (state, data) {
      state.addressList = data
    },
    setDefaultAddrId (state, id) {
      state.defaultAddressId = id
    },
    setRegions (state, data) {
      state.regions = data
    }
  },
  getters: {
    // 全选
    isAllChecked (state) {
      return state.cartList.every(item => item.isChecked)
    },
    cartTotal (state) {
      return state.cartList.reduce((pre, item) => pre + item.goods_num, 0)
    },
    selCartList (state) {
      return state.cartList.filter(item => item.isChecked)
    },
    selCartNum (state, getters) {
      return getters.selCartList.reduce((pre, item) => pre + item.goods_num, 0)
    },
    selCartPrice (state, getters) {
      return getters.selCartList.reduce((pre, item) => pre + item.goods.goods_price_min * item.goods_num, 0).toFixed(2)
    },
    defaultAddress (state, getters) {
      return getters.formatAddressList.find(item => item.id === state.defaultAddressId)
    },
    formatAddressList (state) {
      return state.addressList.map(item => ({ id: item.address_id, name: item.name, tel: item.phone, address: `${item.region.province}${item.region.city}${item.region.region}${item.detail}`, isDefault: item.address_id === state.defaultAddressId }))
    }
  }
}
